package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    //添加套餐信息
    @PostMapping
    @CacheEvict(cacheNames = "setmealList",key = "#setmealDto.categoryId+ #setmealDto.status")
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功成功");
    }

    //分页
    @GetMapping("/page")
    public R<Page<SetmealDto>> page( Integer page,Integer pageSize,String name){
        R<Page<SetmealDto>> pageR = setmealService.pageWithDish(page, pageSize, name);
        return pageR;
    }
    //删除
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealList",allEntries = true)
    public R<String> delete(Long [] ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");
    }
    //起售和停售
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,Long [] ids){
        setmealService.updateStatus(status,ids);
        return  R.success("状态信息修改成功");
    }
    //修改信息
    @GetMapping("/{id}")
    @CacheEvict(cacheNames = "setmealList",allEntries = true)
    public R<SetmealDto> get(@PathVariable Long id){
        SetmealDto byIdWithDish = setmealService.getByIdWithDish(id);
        return R.success(byIdWithDish);
    }
    //修改信息页面的数据回显
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }
    //查询套餐数据
    @GetMapping("list")
    @Cacheable(cacheNames = "setmealList",key = "#setmeal.categoryId+ #setmeal.status",unless = "#result.data.size()==0")
    public R<List<Setmeal>> list(Setmeal setmeal){
        QueryWrapper<Setmeal> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("category_id",setmeal.getCategoryId());
        queryWrapper.eq("status",setmeal.getStatus());
        queryWrapper.orderByDesc("update_time");
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    @GetMapping("/dish/{id}")
    public R<List<DishDto>> img(@PathVariable Long id){
        Setmeal setmeal = setmealService.getById(id);
        QueryWrapper<SetmealDish> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("setmeal_id",setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        List<DishDto> dishDtoList = new ArrayList<>();
        // 遍历套餐菜品集合 获得dishId 根据id获得dish对象
        for (SetmealDish setmealDish : list) {
            DishDto dishDto = new DishDto();
            Long dishId = setmealDish.getDishId();
            Dish dish = dishService.getById(dishId);
            // 资源拷贝
            BeanUtils.copyProperties(dish,dishDto);
            Integer copies = setmealDish.getCopies();
            dishDto.setCopies(copies);
            //创建条件构造器 用来获取口味信息
            QueryWrapper<DishFlavor> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("dish_id",dish.getId());
            //将口味信息装入dishDto
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper1);
            dishDto.setFlavors(dishFlavorList);
            dishDtoList.add(dishDto);
        }
        return R.success(dishDtoList);
    }

}


